|
|
|
|
|
|
|
To add this functionality to the sample application, place two command buttons named cmdAddRec and cmdDelRec on the form. To make the buttons functional, add the code segments shown in Listing 25.1 to the Click event procedure of the appropriate button. |
|
|
|
|
|
|
|
|
Listing 25.1 DATACNTL.ZIPEnhancing the Sample Program |
|
|
|
|
|
|
|
|
Private Sub cmdAddRec_Click()
dtaMain.Recordset.AddNew
End Sub
Private Sub cmdDelRec_Click()
dtaMain.Recordset.Delete
If Not dtaMain.RecordSet.EOF Then
dtaMain.Recordset.MoveNext
Else
dtaMain.Recordset.MoveLast
End If
End Sub |
|
|
|
|
|
|
|
|
As you'll learn in Chapter 26, when you are adding or editing a record, Visual Basic actually goes through a few stages: |
|
|
|
|
|
|
|
|
1. Add. If you are adding a new record, a blank record is prepared in the copy buffer, which is a special place where records are edited before actually being entered into the database. You then proceed to edit the new record. |
|
|
|
|
|
|
|
|
2. Edit mode. While you are changing the information in a record, you are in edit mode. At this point, the record has been copied to the copy buffer and may be modified by the program, but it has not yet been updated in the underlying database. |
|
|
|
|
|
|
|
|
3. Update. After you finish editing field values, the data in the copy buffer is committed to the database, either when you change the current record or invoke the Update method. |
|
|
|
|
|
|
|
|
As you can see, Listing 25.1 does not enter a command to invoke the Update method. (Updates are performed automatically by the Data control whenever you move to a new record or close the form.) |
|
|
|
|
|
|
|
|
|
You add the MoveNext and MoveLast commands to the Delete button to force a move to a new record. After a record is deleted, it is no longer accessible but still shows on-screen until a move is executed. If you do not force a move and try to access the deleted record, an error occurs.  |
|
|
|
|
|
|
|
|
|
|
Your data entry form should now look like the one shown in Figure 25.9. |
|
|
|
|
|
|
|
|
In addition to supplementing the functionality provided by the Data control buttons, you can replace them entirely. Just set the Visible property of the Data control to False, and add command buttons to perform MoveNext and other navigation methods on the Data control. |
|
|
|
|
|